package com.zbw.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.zbw.common.ask.AskBookComment;
import com.zbw.common.controller.Feign.AuthorFeign;
import com.zbw.common.entity.Author;
import com.zbw.common.entity.Book;
import com.zbw.common.mapper.BookDynamicSqlSupport;
import com.zbw.common.mapper.FBookComment;
import com.zbw.common.mapper.FrontBookMapper;
import com.zbw.common.service.BookService;
import com.zbw.common.vo.BookCommentVO;
import com.zbw.common.vo.BookSpVO;
import io.github.xxyopen.model.page.PageBean;
import io.github.xxyopen.model.page.builder.pagehelper.PageBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;
import tk.mybatis.orderbyhelper.OrderByHelper;

import java.util.Date;

import static com.zbw.common.mapper.BookDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.select.SelectDSL.select;

@Service
@Slf4j
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
    private final FBookComment bookCommentMapper;

    private final FrontBookMapper bookMapper;

    private final AuthorFeign authorFeign;
    @Override
    public PageBean<BookCommentVO> selectComment(AskBookComment ask) {
        PageHelper.startPage(ask.getNum(),ask.getSize());
        OrderByHelper.orderBy("t1.create_time desc");
        return PageBuilder.build(bookCommentMapper.listCommentByPage(ask.getUserid(),ask.getBookId()));
    }
    @Override
    public PageBean<?> searchByPage(BookSpVO params, int page, int pageSize) {

        if (params.getUpdatePeriod() != null) {
            long cur = System.currentTimeMillis();
            long period = params.getUpdatePeriod() * 24 * 3600 * 1000;
            long time = cur - period;
            params.setUpdateTimeMin(new Date(time));
        }

        PageHelper.startPage(page, pageSize);

        if (StringUtils.isNotBlank(params.getSort())) {
            OrderByHelper.orderBy(params.getSort() + " desc");
        }
        return PageBuilder.build(bookMapper.searchByPage(params));

    }

    @Override
    public PageBean<Book> listBookPageByUserId(Long userId, int page, int pageSize) {

        Author author = authorFeign.queryAuthor(userId);
        PageHelper.startPage(page, pageSize);

        SelectStatementProvider selectStatement = select(id, bookName, picUrl, catName, visitCount, yesterdayBuy,
                lastIndexUpdateTime, updateTime, wordCount, lastIndexName, status)
                .from(book)
                .where(authorId, isEqualTo(author.getId()))
                .orderBy(BookDynamicSqlSupport.createTime.descending())
                .build()
                .render(RenderingStrategies.MYBATIS3);
        return PageBuilder.build(bookMapper.selectMany(selectStatement));

    }


}
